/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "Chrono刚体.h"


#include <memory>

using namespace chrono;


//auto my_ground = chrono::ChBodyEasyBox(60, 2, 60, 1.0, true, true);

S_Chrono刚体::S_Chrono刚体(S_物理几何体& geom, S_物理材质& 材质, uint32 key) {
	m_物理引擎枚举 = E_物理引擎::E_物理引擎_Chrono;
	m_Type = E_物理体类型::E_物理体_刚体;

	switch (geom.m_Type) {
	case E_物理碰撞几何类型::E_碰撞几何_方体:
		{
			auto* cube = dynamic_cast<S_Chrono长方体几何体*>(&geom);
			m_刚体 = std::make_shared<ChBodyEasyBox>(cube->m_长宽高.x, cube->m_长宽高.y, cube->m_长宽高.z, 1.0, true, true);
			//auto* a = new chrono::ChBodyEasyBox(60.0f, 2.0f, 60.0f, 1.0f, true, true);
			//m_刚体 = *dynamic_cast<ChBody*>(a);
			/*std::vector<chrono::ChVector<>> m_Points;
			m_Points.resize(8);
			for (uint8 z = 0; z < 2; ++z) {
				for (uint8 y = 0; y < 2; ++y) {
					for (uint8 x = 0; x < 2; ++x) {
						uint16 id = (4 * z) + (2*y) + x;
						m_Points[id] = { float64(x), float64(y), float64(z) };
					}
				}
			}
			m_刚体 = std::make_shared<chrono::ChBodyEasyConvexHull>(m_Points, 1.0);*/
		}
		break;
	case E_物理碰撞几何类型::E_碰撞几何_凸壳:
		{
			S_Chrono凸壳几何体* g = dynamic_cast<S_Chrono凸壳几何体*>(&geom);
			m_刚体 = std::make_shared<chrono::ChBodyEasyConvexHull>(g->m_Points, g->m_密度, true);
			//auto b = new chrono::ChBodyEasyConvexHull(g->m_Points, g->m_密度);
		}
		//m_刚体 = chrono::ChBodyEasyConvexHull(g->m_Points, g->m_密度);
		break;
	default:
		break;
	}
	//auto a = new chrono::ChBodyEasyBox(60, 2, 60, 1.0, true, true);
	//m_刚体 = chrono::ChBodyEasyConvexHull();
	//m_刚体.AddMarker();
	m_刚体->GetMaterialSurfaceNSC()->SetFriction(0.5);
	m_刚体->SetCollide(true);

	

	
}

S_Chrono刚体::~S_Chrono刚体()
{
	auto sys = m_刚体->GetSystem();
	if (sys) {
		sys->RemoveBody(m_刚体);
	}
}

void S_Chrono刚体::f_setLocRot(const vec3& loc, const vec3& r) {
	m_刚体->SetPos({ loc.x, loc.y, loc.z });
	vec4 q = f_欧拉角转四元数(r);
	m_刚体->SetRot({ q.w, q.x, q.y, q.z });
}

void S_Chrono刚体::f_setLoc(const vec3& l)
{
}

void S_Chrono刚体::f_setRot(const vec3& r)
{
}

void S_Chrono刚体::f_setVel(const vec3& v) {
	m_刚体->SetWacc_loc({ v.x, v.y, v.z });
}

vec3 S_Chrono刚体::f_getLocation()
{
	return vec3();
}














S_Chrono静态::S_Chrono静态(S_物理几何体& geom, S_物理材质& 材质, uint32 key) {
	m_物理引擎枚举 = E_物理引擎::E_物理引擎_Chrono;
	m_Type = E_物理体类型::E_物理体_静态;

	switch (geom.m_Type) {
	case E_物理碰撞几何类型::E_碰撞几何_方体:
	{
		auto* cube = dynamic_cast<S_Chrono长方体几何体*>(&geom);
		m_刚体 = std::make_shared<ChBodyEasyBox>(cube->m_长宽高.x, cube->m_长宽高.y, cube->m_长宽高.z, 0.0, true, true);
	}
	break;
	case E_物理碰撞几何类型::E_碰撞几何_凸壳:
	{
		S_Chrono凸壳几何体* g = dynamic_cast<S_Chrono凸壳几何体*>(&geom);
		m_刚体 = std::make_shared<chrono::ChBodyEasyConvexHull>(g->m_Points, 0.0f, true);
		//auto b = new chrono::ChBodyEasyConvexHull(g->m_Points, g->m_密度);
	}
	//m_刚体 = chrono::ChBodyEasyConvexHull(g->m_Points, g->m_密度);
	break;
	default:
		break;
	}
	//auto a = new chrono::ChBodyEasyBox(60, 2, 60, 1.0, true, true);
	//m_刚体 = chrono::ChBodyEasyConvexHull();
	//m_刚体.AddMarker();
	//m_刚体->GetMaterialSurfaceNSC()->SetFriction(0.5);
	m_刚体->SetNoSpeedNoAcceleration();
	m_刚体->SetCollide(true);
	m_刚体->SetBodyFixed(true);
	//m_刚体->SetLimitSpeed(true);
	
}

S_Chrono静态::~S_Chrono静态() {

}

void S_Chrono静态::f_setLocRot(const vec3& loc, const vec3& r) {
	m_刚体->SetPos({ loc.x, loc.y, loc.z });
	vec4 q = f_欧拉角转四元数(r);
	m_刚体->SetRot({ q.w, q.x, q.y, q.z });
}

void S_Chrono静态::f_setLoc(const vec3& l) {

}

void S_Chrono静态::f_setRot(const vec3& r) {

}

void S_Chrono静态::f_setVel(const vec3& v) {
	m_刚体->SetWacc_loc({ v.x, v.y, v.z });
}

vec3 S_Chrono静态::f_getLocation() {

	return vec3();
}
